package gui.awt.listener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        new MyCalculator();
    }
}

//计算起来类

class MyCalculator extends Frame{
    public MyCalculator(){

        // 3个文本框
        TextField t1 = new TextField(10);
        TextField t2 = new TextField(10);
        TextField t3 = new TextField(20);

        MyCalcListener myCalcListener = new MyCalcListener(t1,t2,t3);


        // 1个按钮

        Button btn = new Button("=");
        btn.addActionListener(myCalcListener);
        // 1个标签
        Label label = new Label("+");

        //流式布局
        setLayout(new FlowLayout());
        add(t1);
        add(label);
        add(t2);
        add(btn);
        add(t3);

        pack();
        setVisible(true);

    }
}


//监听器类
class MyCalcListener implements ActionListener{

    private TextField num1,num2,num3;
    public MyCalcListener(TextField t1, TextField t2, TextField t3) {
        this.num1 = t1;
        this.num2 = t2;
        this.num3 = t3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = 0;
        int n2 = 0;
        int n3 = 0;
        n1 = Integer.parseInt(num1.getText());
        n2 = Integer.parseInt(num2.getText());
        n3 = n1 + n2;

        num3.setText("" + n3);

        num1.setText("");
        num2.setText("");
    }
}